package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.SpendPointItem;
import cn.wolfcode.crm.query.JSONResult;
import cn.wolfcode.crm.query.SpendPointQueryObject;
import cn.wolfcode.crm.service.ISpendPointItemService;
import cn.wolfcode.crm.util.JSONUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("spendPointItem")
public class SpendPointItemController {

    @Autowired
    private ISpendPointItemService spendPointItemService;

    //页面数据
    @RequestMapping("query")
    @ResponseBody
    public List<SpendPointItem> query(SpendPointQueryObject qo){
        return spendPointItemService.selectAll(qo);
    }


    //名称检验
    @RequestMapping("validateByName")
    @ResponseBody
    public Boolean validateByName(Long id, String name){
        return spendPointItemService.selectByName(id,name);
    }

    //编辑
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JSONResult saveOrUpdate(SpendPointItem spendPointItem){
        JSONResult result = new JSONResult();
        try{
            spendPointItemService.saveOrUpdate(spendPointItem);
        }catch (Exception e){
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }
    //删除
    @RequestMapping("delete")
    @ResponseBody
    public JSONResult delete(Long id){
        JSONResult result = new JSONResult();
        try{
            spendPointItemService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }



    //导出
    @RequestMapping("export")
    public void export(SpendPointQueryObject qo, HttpServletResponse response) throws IOException {
        //设置响应头
        response.setHeader("Content-Disposition", "attachment;filename=spendPoint.xls");
        //创建文件对象
        Workbook wb = new HSSFWorkbook();
        //创建工作簿
        Sheet sheet = wb.createSheet("spendPointData");
        //查询明细数据(要带高级查询的参数)
        List<SpendPointItem> list = spendPointItemService.selectExportData(qo);
        //创建第一行
        Row row = sheet.createRow(0);
        //设置标题
        row.createCell(0).setCellValue("支出名称");
        row.createCell(1).setCellValue("支出金额");
        row.createCell(2).setCellValue("支出时间");
        row.createCell(3).setCellValue("支出人");
        row.createCell(4).setCellValue("简介");
        //循环数据
        for (int i = 0; i < list.size(); i++) {
            //从第二行开始创建行
            Row row1 = sheet.createRow(i + 1);
            SpendPointItem pointItem = list.get(i);
            //创建单元格
            row1.createCell(0).setCellValue(pointItem.getName());
            row1.createCell(1).setCellValue(pointItem.getSpendamount().toString());
            row1.createCell(2).setCellValue(pointItem.getSpenddate());
            row1.createCell(3).setCellValue(pointItem.getSpenduser());
            row1.createCell(4).setCellValue(pointItem.getInfo());
        }

        //输出到单元格
        wb.write(response.getOutputStream());
    }


    //导入
    @RequestMapping("importData")
    public String importData(MultipartFile file) throws IOException {
        spendPointItemService.importData(file);
        return "redirect:/spendPointItem/view.do";
    }


    @RequestMapping("selectAmountByDay")
    @ResponseBody
    public Object selectAmountByDay() throws ParseException {
        return spendPointItemService.selectAmountByDay();
    }

    @RequestMapping("selectAmountByMouth")
    @ResponseBody
    public Object selectAmountByMouth() throws ParseException {
        return spendPointItemService.selectAmountByMouth();
    }

    @RequestMapping("selectAllAmount")
    @ResponseBody
    public Object selectAllAmount() throws ParseException {
        return spendPointItemService.selectAllAmount();
    }

    @RequestMapping("selectSpendTimes")
    @ResponseBody
    public Object selectSpendTimes() throws ParseException {
        return spendPointItemService.selectSpendTimes();
    }


    @RequestMapping("querybyDate")
    @ResponseBody
    public List<SpendPointItem> querybyDate(Date beginDate,Date endDate) throws ParseException {
        return spendPointItemService.querybyDate(beginDate,endDate);
    }

    @RequestMapping("queryAvgSpend")
    @ResponseBody
    public Object queryAvgSpend() throws ParseException {
        return spendPointItemService.queryAvgSpend();
    }


    @RequestMapping("queryMaxSpend")
    @ResponseBody
    public Object queryMaxSpend() throws ParseException {
        return spendPointItemService.queryMaxSpend();
    }

    /*@RequestMapping("query")
    public String view(){
        return "spendAnalyze";
    }*/

    //页面数据
    @RequestMapping("list")
    @ResponseBody
    public List<Map<String, Object>> list(SpendPointQueryObject qo) {
        return spendPointItemService.items(qo);
    }
    
    @RequestMapping("spendAnalyze")
    public String spendByBar(@ModelAttribute("qo") SpendPointQueryObject qo, Model model){
        List<Object> x = new ArrayList<>();
        List<Object> y = new ArrayList<>();
        List<Object> z = new ArrayList<>();
        List<Object> h = new ArrayList<>();
        //先获取集合里的数据
        List<Map<String,Object>> maps = spendPointItemService.items(qo);
        List<Map<String,Object>> zhis = spendPointItemService.zitems(qo);
        //然后把集合迭代出来得到map
        for (Map<String, Object> map : maps) {
            if (map != null ){
                //取出当前行中的name和销售金额两个值存入x,y集合中
                x.add(map.get("spenddate"));
                y.add(map.get("spendamount"));

            }
        }
        for (Map<String, Object> zhi : zhis) {
            if (zhi != null){
                z.add(zhi.get("name"));
                h.add(zhi.get("spendamount"));
            }
        }
        //转换成json,传递参数
        model.addAttribute("x", JSONUtil.toJSONString(x));
        model.addAttribute("y",JSONUtil.toJSONString(y));
        model.addAttribute("z", JSONUtil.toJSONString(z));
        model.addAttribute("h",JSONUtil.toJSONString(h));
        return "spendAnalyze";


    }
}
